package com.atguigu.juc01;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Ticket //类 = Field + method
{
    private int number = 50;
    /*public synchronized void sale()
    {
        if(number > 0)
        {
            System.out.println(Thread.currentThread().getName()+"\t"+"卖出第: "+(number--)+"\t 还剩下: "+number);
        }
    }*/

    //=---===============JUC

    Lock lock = new ReentrantLock();// List list =new ArrayList();
    public void sale()
    {
        lock.lock();
        try
        {
            if(number > 0)
            {
                System.out.println(Thread.currentThread().getName()+"\t"+"卖出第: "+(number--)+"\t 还剩下: "+number);
            }
        }finally {
            lock.unlock();
        }
    }
}


/**
 * @auther zzyy
 * @create 2020-03-25 12:41
 * 三个售票员    卖出      50张票
 * 如何编写企业级需要的工程化代码？
 *
 * 1 在高内聚低耦合的前提下，线程 操作  资源类,多线程开发中，一言不合先new一个资源类,线程对资源类进行什么样的操作？
 */
public class SaleTicketDemo
{
    public static void main(String[] args)//一切程序的入口
    {
        Ticket ticket = new Ticket();

        //Thread(Runnable target, String name) Allocates a new Thread object.
        /*new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                for (int i = 0; i < 51; i++) {
                    ticket.sale();
                }
            }
        }, "A").start();

        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                for (int i = 0; i < 51; i++) {
                    ticket.sale();
                }
            }
        }, "B").start();

        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                for (int i = 0; i < 51; i++) {
                    ticket.sale();
                }
            }
        }, "C").start();*/


        new Thread(() -> { for (int i = 0; i < 51; i++) ticket.sale(); },"A").start();
        new Thread(() -> { for (int i = 0; i < 51; i++) ticket.sale(); },"B").start();
        new Thread(() -> { for (int i = 0; i < 51; i++) ticket.sale(); },"C").start();
    }
}
